﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

public class WeaponSpriteData
{
    
    public Sprite m_WeaponSprite;
    public Dictionary<int, Sprite> m_FlyingItemSprites = new Dictionary<int, Sprite>();
    public Sprite GetFlyItemSprite(int flyItemID)
    {
        foreach(var i in m_FlyingItemSprites)
        {
            if(i.Key == flyItemID)
            {
                return i.Value;
            }
        }
        return null;
    }
}

public class ResourcesManager
{
    public string ConfigPath;
    public static ResourcesManager Instance = new ResourcesManager();

    List<EngineObject> m_EnginePrefabs = new List<EngineObject>();
    List<WeaponSlotObject> m_WeaponSlotPrefabs = new List<WeaponSlotObject>();
    Dictionary<int, WeaponSpriteData> m_WeaponsSprite = new Dictionary<int, WeaponSpriteData>();
    public Dictionary<string, ExplotionObject> m_ExplotionEffects = new Dictionary<string, ExplotionObject>();
    GameObject m_TheResourceObject;
    
    public ResourcesManager()
    {
        ConfigPath = "";
    }

    public void InitSystem()
    {
        m_TheResourceObject = new GameObject("ResourceManager");
        GameObject.DontDestroyOnLoad(m_TheResourceObject);
        //  引擎的特效
        var object2 = Resources.LoadAll(Utils._ENGINE_EFFECT_PATH_);
        for (int i = 0; i < object2.Length; ++i)
        {
            var gobj = object2[i] as GameObject;
            if (gobj != null)
                m_EnginePrefabs.Add(gobj.GetComponent<EngineObject>());
        }
        //  爆炸特效
        var object3 = Resources.LoadAll(Utils._EXPLOTION_EFFECT_PATH_);
        for (int i = 0; i < object3.Length; ++i)
        {
            var gobj = object3[i] as GameObject;
            if (gobj != null)
            {
                m_ExplotionEffects.Add(gobj.name, gobj.GetComponent<ExplotionObject>());
            }
        }

        // 武器的贴图
        foreach(var i in ItemManager.Instance.m_Weapons)
        {
            AddWeaponResources(i);
        }

        //  武器槽
        foreach(var i in ItemManager.Instance.m_WeaponSlots)
        {
            GameObject gameObj = new GameObject();
            gameObj.transform.SetParent(m_TheResourceObject.transform);
            var weaponSlotObj = gameObj.AddComponent<WeaponSlotObject>();
            var render = gameObj.AddComponent<SpriteRenderer>();
            weaponSlotObj.typeID = i.typeID;
            render.sprite = Utils.LoadSpriteByIo(Utils.GetDataPath() + i.iconPath, new Vector2(0.5f, 0.5f));
            render.sortingOrder = Utils._PARTS_RENDER_ORDER_;
            m_WeaponSlotPrefabs.Add(weaponSlotObj);
            //GameObject.DontDestroyOnLoad(gameObj);
            //gameObj.SetActive(false);
            gameObj.name = "WeaponSlot";
            gameObj.SetActive(false);
        }
    }
    public WeaponSlotObject GetWeaponSlotObjectPrefab(int typeID)
    {
        foreach(var i in m_WeaponSlotPrefabs)
        {
            if(i.typeID == typeID)
            {
                return i;
            }
        }
        return null;
    }
    public void AddWeaponResources(Weapon weapon)
    {
        var sprite = Utils.LoadSpriteByIo(Utils.GetDataPath() + weapon.iconPath, weapon.pivot);

        WeaponSpriteData data = new WeaponSpriteData();
        data.m_WeaponSprite = sprite;

        //  子弹
        foreach (var j in weapon.flyItems)
        {
            sprite = Utils.LoadSpriteByIo(Utils.GetDataPath() + j.iconPath, weapon.pivot, 200);
            data.m_FlyingItemSprites.Add(j.flyItemID, sprite);
        }

        m_WeaponsSprite.Add(weapon.typeID, data);
    }
    public ExplotionObject GetExplotionEffectPrefab(string effectName)
    {
        if(m_ExplotionEffects.ContainsKey(effectName))
            return m_ExplotionEffects[effectName];
        return null;
    }
    public Sprite GetItemSprite(Item item)
    {
        if(item as Weapon == null)
        {
            MessageShow.Instance.Error("the item's sprite have not be add to the resources Manager.");
            return null;
        }
        return GetWeaponSprite(item.typeID);
    }
    public Sprite GetWeaponSprite(int typeID)
    {
        if (m_WeaponsSprite.ContainsKey(typeID))
            return m_WeaponsSprite[typeID].m_WeaponSprite;
        return null;
    }
    public void SetWeaponSprite(int typeID, Sprite sprite)
    {
        if (m_WeaponsSprite.ContainsKey(typeID))
            m_WeaponsSprite[typeID].m_WeaponSprite = sprite;
        else
        {
            WeaponSpriteData data = new WeaponSpriteData();
            data.m_WeaponSprite = sprite;
            m_WeaponsSprite.Add(typeID, data);
        }
    }
    public void SetWeaponsFlyItemSprite(int typeID, int flyItemID, Sprite sprite)
    {
        if (m_WeaponsSprite.ContainsKey(typeID))
        {
            if (m_WeaponsSprite[typeID].m_FlyingItemSprites.ContainsKey(flyItemID))
            {
                m_WeaponsSprite[typeID].m_FlyingItemSprites[flyItemID] = sprite;
            }
            else
                m_WeaponsSprite[typeID].m_FlyingItemSprites.Add(flyItemID, sprite);
        }
        else
        {
            WeaponSpriteData data = new WeaponSpriteData();
            data.m_FlyingItemSprites.Add(flyItemID, sprite);
            m_WeaponsSprite.Add(typeID, data);
        }
    }
    public Sprite GetWeaponsFlyItemSprite(int typeID, int flyItemID)
    {
        var data = GetWeaponSpriteData(typeID);
        if (data == null)
            return null;
        return data.GetFlyItemSprite(flyItemID); 
    }
    public WeaponSpriteData GetWeaponSpriteData(int typeID)
    {
        if (m_WeaponsSprite.ContainsKey(typeID))
        {
            return m_WeaponsSprite[typeID];
        }
        return null;
    }
    public void SaveWeapon(Weapon weapon)
    {
        //SetWeaponSprite(weapo n.m_TypeID, );
        var data = GetWeaponSpriteData(weapon.typeID);
        Sprite sprite = data.m_WeaponSprite;
        if (sprite != null)
        {
            if(weapon.iconPath == "")
                weapon.iconPath = "Textures/Items/Weapon/" + weapon.level.ToString()+"/" + weapon.m_Type.ToString()+"/" + weapon.typeID + ".png";

            if (Utils.SaveImage(sprite.texture, Utils.GetDataPath() + weapon.iconPath) == false)
                MessageShow.Instance.Error("武器外观设计图保存失败，可能是错误的路径导致。");
        }
        else
        {
            MessageShow.Instance.Error("武器外观设计图保存失败，没有找到图片。");
        }
        
        foreach(var i in weapon.flyItems)
        {
            if(data.m_FlyingItemSprites.ContainsKey(i.flyItemID))
            {
                var flyItemSprite = data.m_FlyingItemSprites[i.flyItemID];
                if (flyItemSprite != null)
                {
                    if(Utils.SaveImage(flyItemSprite.texture, Utils.GetDataPath() + i.iconPath) == false)
                        MessageShow.Instance.Error("武器子弹外观设计图保存失败，可能是错误的路径导致。");
                }
                else
                {
                    MessageShow.Instance.Error("武器子弹外观设计图保存失败，没有找到图片。");
                }
            }
        }
            
    }

    public EngineObject GetEnginePrefab(Engine.EngineType type)
    {
        foreach(var i in m_EnginePrefabs)
        {
            if(i.m_EngineType == type)
            {
                return i;
            } 
        }
        return null; 
    }
}